import cv2
import sys
import os

from .converter import Converter

class MyQrcode:
    def __init__(self):
        pass

    def main(self, file_paths):

        if not self.check_file_path(file_paths):
            print('File not found.')
            sys.exit(-1)

        migration_list = self.get_str_from_qr_code(file_paths)
        plain_list = self.convert(migration_list)

        for each_plain in plain_list:
            print(each_plain)

    def getPlainPath(self, file_path):
        if not self.check_file_path([file_path]):
            print('File not found.')
            sys.exit(-1)

        migration_list = self.get_str_from_qr_code([file_path])
        if (migration_list[0].startswith("otpauth:")):
           plain_list = migration_list
        else:
            try:
                plain_list = self.convert(migration_list)
            except Exception as e:
                accountList = eval(migration_list[0])
                plain_list = ['otpauth://totp/sangfor:{}?secret={}'.format(accountList[0]["account"],accountList[0]["secret"])]


        return plain_list[0]

    def getSecretAndAccount(self, file_path):
        plainPath = self.getPlainPath(file_path)
        s1 = plainPath.split("?")
        account = s1[0].split(":")[-1].replace("//totp/","")
        secret = s1[1].split("&")[0].split("=")[-1]
        return account, secret

    def convert(self, migration_list):
        plain_list = []
        for migration in migration_list:
            converter = Converter()
            converter.from_migration_string(migration)
            each_plain_list = converter.to_plain_string()
            plain_list.extend(each_plain_list)

        return plain_list


    def get_str_from_qr_code(self, file_paths):

        migration_list = []
        for each_file_path in file_paths:
            migration_str = self.read_qr_code(each_file_path)
            migration_list.append(migration_str)

        return migration_list


    def read_qr_code(self, file_path):
        qr_file = cv2.imread(file_path)
        qr_detector = cv2.QRCodeDetector()
        result = qr_detector.detectAndDecode(qr_file)

        return result[0]


    def check_file_path(self, file_path):
        for each_file_path in file_path:
            if not os.path.isfile(each_file_path):
                print(f'File not found: {each_file_path}')
                return False

        return True


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('Usage: python qrcode.py <filename1> [<filename2>] [<filename3>] ...')
        sys.exit(-1)
    myQrcode = MyQrcode()
    print(sys.argv)
    MyQrcode(sys.argv[1:])

